var page = 1;
var theWd = '';
var loading = false;  //状态标记
var playListData = {};
var content = '';

$(function () {
    $(document.body).infinite(50).on("infinite", function () {
        if (loading) return;
        loading = true;
        searchVideo();
    });

    $('.v-list').on('click', '.vlist', function () {
        loading = true;
        var vodid = $(this).attr('data-vodid');
        var theData = playListData[vodid];
        $('.v-list').html('');
        var listHtml = '';
        for (key in theData) {
            listHtml += '<a data-vodid="' + vodid + '" data-playindex="' + theData[key]['playindex'] + '" class="weui-cell weui-cell_access jlist" href="javascript:;">' +
                '<div class="weui-cell__bd" >' +
                '<p>' + theData[key]['play_name'] + '</p>' +
                '</div> ' +
                '<div class="weui-cell__ft" ></div > ' +
                '</a>';
        }
        $('.j-list').html(listHtml);
    });

    var clipboard = new Clipboard('.cpb', {
        text: function () {
            return content;
        }
    });

    $('.j-list').on('click', '.jlist', function () {
        var vodid = $(this).attr('data-vodid');
        var playindex = $(this).attr('data-playindex');

        var url = 'https://android.leleapps.com/vod/reqplay/' + vodid + '?playindex=' + playindex;
        $.ajax({
            type: 'get',
            dataType: 'json',
            processData: false, // 告诉jquery不要处理数据
            contentType: false, // 告诉jquery不要设置contentType
            url: url,
            success: function (json, textStatus, xhr) {
                console.log(json)
                var httpurl = json.data.httpurl
                if(httpurl == undefined){
                    httpurl = json.data.httpurl_preview
                }   
                $.modal({
                    title: "操作",
                    text: httpurl,
                    buttons: [
                        {
                            text: "打开", onClick: function () {
                                window.open(httpurl, '_blank');
                            }
                        },
                        {
                            text: "复制", onClick: function () {
                                content = httpurl
                                $('.cpb').trigger('click');
                            }
                        },
                        { text: "取消", className: "default", onClick: function () { } },
                    ]
                });

            },
            error: function (xhr, textStatus, errorThrown) { }
        })


    });


});

$("form").submit(function (e) {
    e.preventDefault();
    loading = false;
    page = 1;
    $('.v-list').html('<div class="weui-loadmore"><i class= "weui-loading" ></i><span class="weui-loadmore__tips">正在加载</span></div >');
    $('.j-list').html('')
    searchVideo();
    playListData = {};
});


function searchVideo() {
    var wd = $('.wd').val();
    theWd = wd;
    console.log('page:' + page)
    var url = 'https://android.xiaoxiaoapps.com/search?page=' + page + '&wd=' + wd;
    $.ajax({
        type: 'get',
        dataType: 'json',
        processData: false, // 告诉jquery不要处理数据
        contentType: false, // 告诉jquery不要设置contentType
        url: url,
        success: function (json, textStatus, xhr) {
            var jsonData = json.data.vodrows
            console.log(jsonData)
            var listHtml = '';
            for (key in jsonData) {
                listHtml +=
                    '<a data-vodid="' + jsonData[key]['vodid'] + '" href="javascript:void(0);" class="weui-media-box weui-media-box_appmsg vlist">' +
                    '<div class="weui-media-box__hd" >' +
                    '<img class="weui-media-box__thumb" src="' + jsonData[key]['coverpic'] + '">' +
                    '</div>' +
                    '<div class="weui-media-box__bd">' +
                    '<h4 class="weui-media-box__title">' + jsonData[key]['title'] + '</h4>' +
                    '<p class="weui-media-box__desc">' + jsonData[key]['intro'] + '</p>' +
                    '</div>' +
                    '</a >';
                playListData[jsonData[key]['vodid']] = jsonData[key]['playlist']
            }
            if (page == 1) {
                $('.v-list').html(listHtml);
            } else {
                $('.v-list').append(listHtml);
            }
            if (listHtml != '') {
                page++;
                loading = false;
            } else {
                if (page == 1) {
                    listHtml = '<div class="weui-loadmore weui-loadmore_line"><span class="weui-loadmore__tips">暂无数据</span></div>'
                    $('.v-list').html(listHtml);
                } else {
                    listHtml = '<div class="weui-loadmore weui-loadmore_line weui-loadmore_dot"><span class="weui-loadmore__tips"></span></div>'
                    $('.v-list').append(listHtml);
                }
            }
        },
        error: function (xhr, textStatus, errorThrown) { }
    })
}

